#include <iostream>
#include <cstdlib>
#include <unistd.h>

using namespace std;

bool GetQueryString(std::string &query_string)
{
  string method = getenv("METHOD");
  if (method  == "GET")
  {
    query_string = getenv("QUERY_STRING");
    cerr << "Debug QUERY_STRING: " << query_string << endl; 
  }
  else if (method == "POST")
  {
    int cl = atoi(getenv("CONTENT_LENGTH"));
    char c = 0;
    while(cl)
    {
      read(0 , &c , 1);
      query_string.push_back(c);
      cl--;
    }
  }
  else 
  {
    return false;
  }

  return true;
}

void CutString(string& in, const string& sep, string& out1 , string& out2)
{
  auto pos = in.find(sep);
  if (pos == std::string::npos)
  {
    return;
  }

  out1 = in.substr(0 , pos);
  out2 = in.substr(pos + sep.size());
}


int main()
{
  std::string query_string;
  GetQueryString(query_string); 
  // a=100&b=200

  string str1;
  string str2;
  CutString(query_string , "&" , str1 , str2);

  string name1;
  string value1;
  CutString(str1 , "=" , name1 , value1);

  string name2;
  string value2;
  CutString(str2 , "=" , name2 , value2);

  cout << name1 << ":" << value1 << endl;
  cout << name2 << ":" << value2 << endl;

  // 2 
  
  cerr << name1 << ":" << value1 << endl;
  cerr << name2 << ":" << value2 << endl;
  return 0;
}
